package org.deadline.model.config;

import flexjson.JSONDeserializer;
import flexjson.JSONSerializer;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.SequenceGenerator;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.deadline.model.baseapp.BaseEntity;
import org.deadline.model.baseapp.Organization;
import org.deadline.model.user.Person;
import org.hibernate.annotations.Generated;
import org.hibernate.annotations.GenerationTime;

@Entity
public class ApplicationConfiguration extends BaseEntity  implements Serializable {

    @NotNull
    @Size(min = 3, max = 255)
    private String configKey;

    @Size(max = 255)
    private String configValues;

    @NotNull
    @Size(min = 3, max = 255)
    private String configSection;

    @NotNull
    @Size(min = 10, max = 255)
    private String description;

    @NotNull
    private boolean enabled;

    @ManyToOne
    private Organization organization;

    @ManyToOne
    private Person owner;

	@Id
	@Column(name="id",unique=true,nullable=false)
	@SequenceGenerator(name="application_configuration_seq_gen",sequenceName="application_configuration_seq")
	@GeneratedValue(generator="application_configuration_seq_gen",strategy=GenerationType.TABLE)
	@Generated(GenerationTime.INSERT)
	private Long id;

	
	public String toJson() {
        return new JSONSerializer().exclude("*.class").deepSerialize(this);
    }

	public static ApplicationConfiguration fromJsonToApplicationConfiguration(String json) {
        return new JSONDeserializer<ApplicationConfiguration>().use(null, ApplicationConfiguration.class).deserialize(json);
    }

	public static String toJsonArray(Collection<ApplicationConfiguration> collection) {
        return new JSONSerializer().exclude("*.class").deepSerialize(collection);
    }

	public static Collection<ApplicationConfiguration> fromJsonArrayToApplicationConfigurations(String json) {
        return new JSONDeserializer<List<ApplicationConfiguration>>().use(null, ArrayList.class).use("values", ApplicationConfiguration.class).deserialize(json);
    }

	public String getConfigKey() {
        return this.configKey;
    }

	public void setConfigKey(String configKey) {
        this.configKey = configKey;
    }

	public String getConfigValues() {
        return this.configValues;
    }

	public void setConfigValues(String configValues) {
        this.configValues = configValues;
    }

	public String getConfigSection() {
        return this.configSection;
    }

	public void setConfigSection(String configSection) {
        this.configSection = configSection;
    }

	public String getDescription() {
        return this.description;
    }

	public void setDescription(String description) {
        this.description = description;
    }

	public boolean isEnabled() {
        return this.enabled;
    }

	public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

	public Organization getOrganization() {
        return this.organization;
    }

	public void setOrganization(Organization organization) {
        this.organization = organization;
    }

	public Person getOwner() {
        return this.owner;
    }

	public void setOwner(Person owner) {
        this.owner = owner;
    }

	private static final long serialVersionUID = 1L;

	public String toString() {
        return ReflectionToStringBuilder.toString(this, ToStringStyle.SHORT_PREFIX_STYLE);
    }

	public Long getId() {
	    return this.id;
	}

	public void setId(Long id) {
	    this.id = id;
	}

	
}
